// mircoApp/broadnet-card/broadnetCard.js
const app = getApp();
Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    name: "",
    mobile: "",
    identity_card: "", //身份证号
    province_name: "",
    city_name: "",
    district_name: "",
    province_id: "",
    city_id: "",
    region_id: "", //区编号
    address: "",
    choose_phone_number: "", //选号
    cascaderValue: "",
    show: false,
    optiopns: [],
    fieldNames: {
      text: "area_name",
      value: "id",
      children: "children"
    },
    area_byname: null,
    showNumber: false,
    numberList: []
  },
  lifetimes: {
    attached() {
      this.setData({
        choose_phone_number: ""
      })
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    is_valid(val, msg, data = null) {
      return {
        val,
        msg,
        data
      };
    },
    postData() {
      // 父组件调用ref取得子组件的组装值
      if (app._isTextEmpty(this.data.name)) {
        return this.is_valid(false, "请输入姓名");
      }
      if (app._isTextEmpty(this.data.mobile)) {
        return this.is_valid(false, "请输入手机");
      }
      if (app._isTextEmpty(this.data.identity_card)) {
        return this.is_valid(false, "请输入身份证号码");
      }
      if (app._isTextEmpty(this.data.region_id)) {
        return this.is_valid(false, "请选择地区");
      }
      if (app._isTextEmpty(this.data.address)) {
        return this.is_valid(false, "请输入详细地址");
      }
      if (app._isTextEmpty(this.data.choose_phone_number)) {
        return this.is_valid(false, "请选择号码");
      }
      let broadnet_card_info = {
        name: this.data.name,
        mobile: this.data.mobile,
        identity_card: this.data.identity_card,
        province_name: this.data.province_name,
        city_name: this.data.city_name,
        district_name: this.data.district_name,
        province_id: this.data.province_id,
        city_id: this.data.city_id,
        region_id: this.data.region_id,
        address: this.data.address,
        choose_phone_number: this.data.choose_phone_number
      };
      return this.is_valid(true, "", broadnet_card_info);
    },
    showClose() {
      this.setData({
        show: false
      })
    },
    showNumberTap() {
      this.getNumber();
    },
    showNumberConfirm() {
      this.showNumberClose()
    },
    showNumberClose() {
      this.setData({
        showNumber: false
      })
    },
    async getNumber() {
      let json = {
        requestParam: {
          code: this.data.city_id
        }
      }
      if (this.data.fuzzyKey) {
        json.requestParam.fuzzyKey = this.data.fuzzyKey;
      }
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss("plugin.broadnet-card.frontend.pre-order.get-number"),
        data: json
      })
      if (data.result) {
        this.setData({
          numberList: data.data.numList,
          showNumber: !this.data.showNumber ? true : this.data.showNumber
        })
      } else {
        app.tips(data.msg)
      }
    },
    tapSelect(e) {
      let {
        accessnum
      } = e.currentTarget.dataset;
      this.setData({
        choose_phone_number: accessnum,
      })
    },
    onChange(e) {
      const {
        tabIndex,
        value,
        selectedOptions
      } = e.detail;
      console.log(e.detail)
      if (tabIndex === 0) {
        this.getChildren(value, tabIndex)
      }
      if (tabIndex === 1) {
        let one_value = selectedOptions[0].id;
        this.getChildren(value, tabIndex, one_value)
      }
      if (tabIndex === 2) {
        if(this.data.choose_phone_number){
          this.setData({
            choose_phone_number: ""
          })
        }
        this.setData({
          province_name: selectedOptions[0].area_name,
          city_name: selectedOptions[1].area_name,
          district_name: selectedOptions[2].area_name,
          province_id: selectedOptions[0].area_code,
          city_id: selectedOptions[1].area_code,
          region_id: selectedOptions[2].area_code,
          cascaderValue: selectedOptions[2].id,
          area_byname: selectedOptions[2].area_byname,
          show: false
        })
      }
    },
    async getChildren(id, tabIndex, one_value) {
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss("plugin.broadnet-card.frontend.pre-order.get-area"),
        data: {
          parent_id: id
        }
      })
      if (data.result) {
        let ind, twoind;
        if (tabIndex === 1) {
          this.data.optiopns.forEach((ele, index) => {
            if (ele.id == one_value) {
              ind = index
              ele.children.forEach((child, ind) => {
                if (child.id == id) {
                  twoind = ind
                }
              })
            }
          });
          this.setData({
            ['optiopns[' + ind + '].children[' + twoind + '].children']: data.data
          })
        } else {
          this.data.optiopns.forEach((ele, index) => {
            if (ele.id == id) {
              ind = index
            }
          });
          data.data.forEach((ele) => {
            ele.children = [];
          })
          this.setData({
            ['optiopns[' + ind + '].children']: data.data
          })
        }
      }
    },
    initShow() {
      this.setData({
        optiopns: [],
        show: false,
        area_byname: "",
        cascaderValue: null
      })
    },
    async showTap() {
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss("plugin.broadnet-card.frontend.pre-order.get-area"),
      })
      if (data.result) {
        this.setData({
          optiopns: data.data,
          show: true
        })
      } else {
        app.tips(data.msg);
      }

    }
  }
})